package itbour.onetouchshow.activity.challengezone.material;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.adapter.BaseRecyclerHolder;
import com.andview.refreshview.XRefreshView;
import com.google.gson.Gson;
import com.ys.uilibrary.swip.SwipeMenuLayout;

import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.base.BaseRefreshActivity;
import itbour.onetouchshow.base.BaseRefreshAdapter;
import itbour.onetouchshow.bean.ModelVideoBean;
import itbour.onetouchshow.bean.MyCollectBean;
import itbour.onetouchshow.bean.VideoBean;
import itbour.onetouchshow.bean.page.Page;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.ImageLoader;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.widget.RoundImageView;


/**
 * 素材列表
 */

public class MaterialActivity extends BaseRefreshActivity<MaterialContract.View, MaterialPresenter> implements MaterialContract.View {

    List<MyCollectBean.ListBean> mdata = new ArrayList<>();
    Page page;

    @Override
    public void loadSucceed(String result) {
        showContentView();
        showXRefreshViewNetWorkSuccessView();
        page.finishLoad(true);
        MyCollectBean myCollectBean = new Gson().fromJson(result, MyCollectBean.class);
        List<MyCollectBean.ListBean> data = myCollectBean.getList();
        if (pullStatus == ON_LOAD) {
            mdata.addAll(data);
            xRefreshView.stopLoadMore();
        } else if (pullStatus == ON_REFRESH) {
            mdata = data;
            xRefreshView.stopRefresh();
        }
        if (myCollectBean != null && mPageIndex == myCollectBean.getTotalPage() - 1) {
            xRefreshView.setLoadComplete(true);
        } else {
            xRefreshView.setLoadComplete(false);
        }
        baseRefreshAdapter.notifyDataSetChanged(mdata);
    }

    @Override
    public void loadFaild(String error) {
        page.finishLoad(false);
    }


    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("素材列表");
    }

    @Override
    public BaseRefreshAdapter getBaseRefreshAdapter() {
        return new Material(getApplicationContext(), mdata, R.layout.item_mycollect_adapter);
    }

    @Override
    public View getHeadView() {
        return null;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        onLoadData();
    }


    private void onLoadData() {
        page = new Page() {
            @Override
            public void load(int param1, int param2) {
                L_.e("load" + param1);
                mPageIndex = param1;
                mPresenter.getDetailsList(MaterialActivity.this, null);
            }

        };
        pullStatus = ON_REFRESH;
        if (mPresenter != null) {
            page.loadPage(true);
        }
    }


    @Override
    protected void initView() {
        super.initView();
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                pullStatus = ON_REFRESH;
                if (mPresenter != null) {
                    page.loadPage(true);
                }
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                UIUtils.getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pullStatus = ON_LOAD;
                        if (mPresenter != null) {
                            page.loadPage(false);
                        }
                    }

                }, AppConst.DELAYED_TIME);

            }
        });
    }

    @Override
    public void loadMateriaDetail(String result) {
        ModelVideoBean modelVideoBean = new Gson().fromJson(result, ModelVideoBean.class);
        VideoBean video = modelVideoBean.getVideo();
        Intent intent = new Intent();
        intent.putExtra(AppConst.VIDEOURL, video.getVideoUrl());
        intent.putExtra("Tempurl", video.getThumbs().get(0));
        setResult(1111, intent);
        finish();
    }


    public class Material extends BaseRefreshAdapter<MyCollectBean.ListBean> {

        public Material(Context context, List<MyCollectBean.ListBean> datas, int itemLayoutId) {
            super(context, datas, itemLayoutId);
        }

        @Override
        protected void convert(BaseRecyclerHolder var1, MyCollectBean.ListBean var2, int var3) {
            SwipeMenuLayout swipeMenuLayout = var1.getView(R.id.id_root_swp);
            TextView tvTime = var1.getView(R.id.id_tv_time);
            TextView tvName = var1.getView(R.id.id_tv_name);
            TextView tvType = var1.getView(R.id.id_tv_type);
            RoundImageView imageView = var1.getView(R.id.id_ig_av);
            LinearLayout btnDelete = var1.getView(R.id.btnDelete_ly);
            tvType.setText("视频");
            ImageLoader.getInstance().showImage(MaterialActivity.this, var2.getThumb(), imageView.getImageView());
            tvName.setText(var2.getName());
            tvTime.setText(var2.getTime());
            var1.getView(R.id.id_root_ly).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.loadThisPageInfo(var2.getId());
                }
            });
        }
    }
}
